# 2025年2月20日10:19:39
# 统计每种标签的数量

import os

def count_lines_starting_with_chars(folder_path):
    # 初始化计数器
    counts = {'0': 0, '1': 0, '2': 0}

    # 遍历文件夹中的所有文件
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.txt'):
                file_path = os.path.join(root, file)
                try:
                    # 打开文件并逐行读取
                    with open(file_path, 'r', encoding='utf-8') as f:
                        for line in f:
                            if line:
                                first_char = line[0]
                                if first_char in counts:
                                    counts[first_char] += 1
                except Exception as e:
                    print(f"Error reading file {file_path}: {e}")

    return counts

# 指定文件夹路径
# folder_path = r'D:\上过的课\2024-2025-1-大四上\毕设\datasets\sum\labels'  # .是当前文件夹，你可以修改为你需要的文件夹路径
#folder_path = r'D:\上过的课\2024-2025-1-大四上\毕设\datasets\VOCdevkit\labels\train'
#folder_path = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\test\labels"
#folder_path = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\augmented\labels"
#folder_path = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\wash1+augmented\labels"
folder_path = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\TWHD\labels"
# 调用函数进行统计
result = count_lines_starting_with_chars(folder_path)

# 输出统计结果
for char, count in result.items():
    print(f"以 '{char}' 开头的行有 {count} 行。")